package cn.xiehang.SoundSight;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class NoticeAdapter extends RecyclerView.Adapter<NoticeAdapter.NoticeViewHolder> {

    private List<FAQItem> faqList;

    public NoticeAdapter(List<FAQItem> faqList) {
        this.faqList = faqList;
    }

    @NonNull
    @Override
    public NoticeAdapter.NoticeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recycler_view_question, parent, false);
        return new NoticeAdapter.NoticeViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NoticeViewHolder holder, int position) {
        FAQItem item = faqList.get(position);
        holder.questionText.setText(item.getQuestion());
        holder.answerText.setText(item.getAnswer());

        holder.itemView.setOnClickListener(v -> {
            boolean isExpanded = item.isExpanded();
            item.setExpanded(!isExpanded);
            holder.answerText.setVisibility(isExpanded ? View.GONE : View.VISIBLE);
        }
        );
    }
    @Override
    public int getItemCount() {
        return faqList.size();
    }

    static class NoticeViewHolder extends RecyclerView.ViewHolder {
        TextView questionText, answerText;

        public NoticeViewHolder(@NonNull View itemView) {
            super(itemView);
            questionText = itemView.findViewById(R.id.questionText);
            answerText = itemView.findViewById(R.id.answerText);
        }
    }
}